package top.mcwebsite.playandroid.business.me.impl.main.ui

import android.os.Bundle
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Scaffold
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowForwardIos
import androidx.compose.material.icons.filled.Bookmark
import androidx.compose.material.icons.filled.Diamond
import androidx.compose.material.icons.filled.Gite
import androidx.compose.material.icons.filled.History
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import coil.compose.AsyncImage
import top.mcwebsite.lib.route.api.EasyRouter
import top.mcwebsite.lib.router.annotation.Route
import top.mcwebsite.playandroid.business.me.impl.main.vm.MeIntent
import top.mcwebsite.playandroid.business.me.impl.main.vm.MeUiEffect
import top.mcwebsite.playandroid.business.me.impl.main.vm.MeUiState
import top.mcwebsite.playandroid.business.me.impl.main.vm.MeViewModel
import top.mcwebsite.playandroid.common.constant.RouteConfig
import top.mcwebsite.playandroid.resources.R as R

@Route(RouteConfig.Me.PATH)
@Composable
fun MePage() {
    val viewModel: MeViewModel = viewModel()
    viewModel.HandleUiEffect {
        when (it) {
            is MeUiEffect.NavigateToLoginEffect -> {
                EasyRouter.navigate(RouteConfig.Login.PATH)
            }

            is MeUiEffect.NavigateToUserCoinPageUiEffect -> {
                EasyRouter.navigate(RouteConfig.Coin.PATH) {
                    withParcelable(RouteConfig.Coin.Param.COIN_INFO, it.coinInfo)
                }
            }

            is MeUiEffect.NavigationToSettingsPageUiEffect -> {
                EasyRouter.navigate(RouteConfig.Settings.PATH)
            }
        }
    }
    Scaffold { innerPadding ->
        val uiState by viewModel.uiStateFlow.collectAsState()
        MePage(
            uiState = uiState,
            modifier = Modifier.padding(innerPadding)
        ) {
            viewModel.sendIntent(it)
        }
    }
}

@Composable
fun MePage(
    uiState: MeUiState,
    modifier: Modifier = Modifier,
    sendIntent: (MeIntent) -> Unit,
) {
    Column(
        modifier = modifier.fillMaxSize()
    ) {
        if (!uiState.isLogin) {
            UnLoginContent {
                sendIntent(MeIntent.LoginIntent)
            }
        } else {
            UserInfoContent(uiState)
        }
        SettingsItem(
            modifier = Modifier
                .clickable {
                    sendIntent(MeIntent.GoUserCoinPageIntent)
                }
                .padding(horizontal = 16.dp, vertical = 10.dp),
            title = "我的积分",
            icon = {
                Icon(Icons.Default.Diamond, contentDescription = null, tint = MaterialTheme.colorScheme.primary)
            },
            rightLabel = {
                if (uiState.coinCount != null) {
                    Text(
                        text = uiState.coinCount.toString(),
                        style = MaterialTheme.typography.labelMedium,
                        color = LocalContentColor.current.copy(alpha = 0.7f),
                        modifier = Modifier.padding(end = 5.dp)
                    )
                }
            },
        )
        SettingsItem(modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp), title = "我的分享") {
            Icon(Icons.Default.Share, contentDescription = null, tint = MaterialTheme.colorScheme.primary)
        }
        SettingsItem(modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp), title = "我的书签") {
            Icon(Icons.Default.Bookmark, contentDescription = null, tint = MaterialTheme.colorScheme.primary)
        }
        SettingsItem(modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp), title = "阅读历史") {
            Icon(Icons.Default.History, contentDescription = null, tint = MaterialTheme.colorScheme.primary)
        }
        SettingsItem(modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp), title = "开源项目") {
            Icon(Icons.Default.Gite, contentDescription = null, tint = MaterialTheme.colorScheme.primary)
        }
        SettingsItem(modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp), title = "关于作者") {
            Icon(Icons.Default.Info, contentDescription = null, tint = MaterialTheme.colorScheme.primary)
        }
        SettingsItem(
            modifier = Modifier
                .clickable {
                    sendIntent(MeIntent.GoSettingsPageIntent)
                }
                .padding(horizontal = 16.dp, vertical = 10.dp),
            title = "系统设置"
        ) {
            Icon(Icons.Default.Settings, contentDescription = null, tint = MaterialTheme.colorScheme.primary)
        }
    }
}

@Composable
fun UnLoginContent(
    goLogin: () -> Unit,
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .height(300.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Spacer(modifier = Modifier.height(60.dp))
        AsyncImage(model = R.mipmap.ic_launcher_round, contentDescription = null)
        Spacer(modifier = Modifier.height(10.dp))
        Button(onClick = { goLogin() }) {
            Text(text = "去登录")
            Icon(
                Icons.Default.ArrowForwardIos,
                modifier = Modifier.size(12.dp),
                contentDescription = null
            )
        }
        Spacer(modifier = Modifier.height(40.dp))
    }
}

@Composable
fun UserInfoContent(uiState: MeUiState) {
    Row(
        modifier = Modifier
            .padding(top = 10.dp, bottom = 20.dp)
            .padding(horizontal = 16.dp)
    ) {
        AsyncImage(
            model = uiState.userAvatar.ifEmpty { R.mipmap.ic_launcher_round },
            contentDescription = null,
            modifier = Modifier.size(60.dp)
        )
        Spacer(modifier = Modifier.width(10.dp))
        Column {
            Text(text = uiState.username, style = MaterialTheme.typography.titleLarge)
            Spacer(modifier = Modifier.height(5.dp))
            Row {
                Text("等级: ${uiState.level}", style = MaterialTheme.typography.labelMedium)
                Box(
                    modifier = Modifier
                        .width(1.dp)
                        .height(5.dp)
                        .background(MaterialTheme.colorScheme.outlineVariant)
                )
                Text("排名: ${uiState.rank}", style = MaterialTheme.typography.labelMedium)
            }
        }
    }
}
